package com.mall.serviceImpl;

import com.mall.bean.SystemRole;
import com.mall.bean.SystemUserRole;
import com.mall.mapper.SystemUserRoleMapper;
import com.mall.service.SystemRoleService;
import com.mall.service.SystemUserRoleService;
import com.mall.vo.NodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @PageName com.mall.serviceImpl
 * @Description 用户-角色
 * @Author 何全裕
 * @Create 2017-09-09 19:19
 * @Version 1.0
 **/
@Service
public class SystemUserRoleServiceImpl implements SystemUserRoleService {

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    @Autowired
    private SystemRoleService systemRoleService;

    public List<NodeVo> getSyetemUserRole(String user_id) {
        List<NodeVo> list = new ArrayList<NodeVo>();
        List<SystemRole> all_list = systemRoleService.getSystemRoleList();
        List<SystemUserRole> role_list = systemUserRoleMapper.getSyetemUserRole(user_id);
        for(SystemRole allrole : all_list){
            boolean flag = false;
            NodeVo nodeVo = new NodeVo();
            nodeVo.setName(allrole.getName());
            nodeVo.setOpen(true);
            nodeVo.setId(allrole.getId());
            nodeVo.setPId("0");
            for(SystemUserRole userRole : role_list){
                if(userRole.getSysRoleId().equals(allrole.getId())){
                    flag = true;
                    break;
                }
            }
            if(flag){
                nodeVo.setChecked(true);
            }else{
                nodeVo.setChecked(false);
            }
            list.add(nodeVo);
        }
        return list;
    }

    public void deleteUserRoleByUserId(String user_id) {
        systemUserRoleMapper.deleteUserRoleByUserId(user_id);
    }

    public void insertUserRole(SystemUserRole userRole) {
        systemUserRoleMapper.insertUserRole(userRole);
    }

    public void deleteUserRoleByRoleId(String role_id) {
        systemUserRoleMapper.deleteUserRoleByRoleId(role_id);
    }
}
